package hk.ust.comp3111.ustnutrition;

import hk.ust.comp3111.ustnutrition.data.DataAccessor;
import hk.ust.comp3111.ustnutrition.data.db.User;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;

public class UserInfoDialog extends DialogFragment
{
	public interface UserInfoDialogListener
	{
		public void onUpdateUserInfo(User user);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState)
	{
		ContextThemeWrapper theme = new ContextThemeWrapper(getActivity(),
				R.style.Theme_AppCompat_Light);
		LayoutInflater inflater = LayoutInflater.from(getActivity())
				.cloneInContext(theme);
		final View dialogView = inflater.inflate(R.layout.user_info_dialog, null);
		initView(dialogView);
		OnClickListener okListener = new OnClickListener()
				{
					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						String name = mNameView.getText().toString();
						boolean isFemale = mFemaleCheckBox.isChecked();
						int age = mAgeBar.getProgress() + AGE_BEGIN;
						int height = mHeightBar.getProgress() + HEIGHT_BEGIN;
						int weight = mWeightBar.getProgress() + WEIGHT_BEGIN;
						User.ExerciseLevel exercise = User.ExerciseLevel.getValue(
								mExerciseSpinner.getSelectedItemPosition());
						boolean isJoinRanking = mJoinRankingCheckBox.isChecked();
						User user = new User(name, isFemale, age, height, weight,
								exercise, isJoinRanking);
						mListener.onUpdateUserInfo(user);
					}
				};

		AlertDialog.Builder builder = new AlertDialog.Builder(theme);
		Dialog d = builder.setTitle(R.string.user_info_title)
				.setView(dialogView)
				.setPositiveButton(android.R.string.ok, okListener)
				.setNegativeButton(android.R.string.cancel, null)
				.create();
		return d;
	}

	@Override
	public void onAttach(Activity activity)
	{
		super.onAttach(activity);
		if (activity instanceof UserInfoDialogListener)
		{
			mListener = (UserInfoDialogListener)activity;
		}
		else
		{
			throw new IllegalStateException("Parent activity must implement UserInfoDialogListener");
		}
	}

	private static final int AGE_BEGIN = 8;
	private static final int HEIGHT_BEGIN = 100;
	private static final int WEIGHT_BEGIN = 20;

	private void initView(View root)
	{
		User user = DataAccessor.Instance.getUser();
		mNameView = (TextView)root.findViewById(R.id.name);
		mNameView.setText(user.getName());

		mFemaleCheckBox = (CheckBox)root.findViewById(R.id.female_check_box);
		mFemaleCheckBox.setChecked(user.isFemale());

		mAgeBar = (SeekBar)root.findViewById(R.id.age_progress);
		final TextView ageValue = (TextView)root.findViewById(R.id.age_value);
		mAgeBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
				{
					@Override
					public void onStopTrackingTouch(SeekBar seekBar)
					{
					}

					@Override
					public void onStartTrackingTouch(SeekBar seekBar)
					{
					}

					@Override
					public void onProgressChanged(SeekBar seekBar, int progress,
							boolean fromUser)
					{
						ageValue.setText("" + (progress + AGE_BEGIN));
					}
				});
		mAgeBar.setProgress(user.getAge() - AGE_BEGIN);

		mHeightBar = (SeekBar)root.findViewById(R.id.height_progress);
		final TextView heightValue = (TextView)root.findViewById(R.id.height_value);
		mHeightBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
				{
					@Override
					public void onStopTrackingTouch(SeekBar seekBar)
					{
					}

					@Override
					public void onStartTrackingTouch(SeekBar seekBar)
					{
					}

					@Override
					public void onProgressChanged(SeekBar seekBar, int progress,
							boolean fromUser)
					{
						heightValue.setText("" + (progress + HEIGHT_BEGIN));
					}
				});

		mHeightBar.setProgress(user.getHeight() - HEIGHT_BEGIN);

		mWeightBar = (SeekBar)root.findViewById(R.id.weight_progress);
		final TextView weightValue = (TextView)root.findViewById(R.id.weight_value);
		mWeightBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
				{
					@Override
					public void onStopTrackingTouch(SeekBar seekBar)
					{
					}

					@Override
					public void onStartTrackingTouch(SeekBar seekBar)
					{
					}

					@Override
					public void onProgressChanged(SeekBar seekBar, int progress,
							boolean fromUser)
					{
						weightValue.setText("" + (progress + WEIGHT_BEGIN));
					}
				});

		mWeightBar.setProgress(user.getWeight() - WEIGHT_BEGIN);

		mExerciseSpinner = (Spinner)root.findViewById(
				R.id.exercise_spinner);
		mExerciseSpinner.setSelection(user.getExercise().getValue());

		mJoinRankingCheckBox = (CheckBox)root.findViewById(
				R.id.join_ranking_check_box);
		mJoinRankingCheckBox.setChecked(user.isJoinRanking());
	}

	private UserInfoDialogListener mListener;

	private TextView mNameView;
	private CheckBox mFemaleCheckBox;
	private SeekBar mAgeBar;
	private SeekBar mHeightBar;
	private SeekBar mWeightBar;
	private Spinner mExerciseSpinner;
	private CheckBox mJoinRankingCheckBox;
}
